<?php

declare(strict_types=1);

namespace app\admin\controller\coupon;

use app\admin\validate\Coupon\Brand as BrandValidate;
use app\model\CouponBrand;
use app\admin\controller\Controller;
use app\model\PageSetting;
use think\facade\Filesystem;
use think\Request;

class Brand extends Controller
{
    use \app\api\controller\traits\coupon_order\Coupon;

    //品牌列表
    public function brandList(){
        $params = input();
        $data = CouponBrand::brandList($params);
        return success('', $data);
    }

    //修改品牌
   public function saveBrand(Request $request){
        $params = $request->only(['id', 'name','image_url','status','sort', 'discount_logo', 'subtitle', 'serv_type']);
        if (isset($params['serv_type']) && !empty($serv_type = $params['serv_type']) && in_array($serv_type, [PRODUCT_TRAVEL_OIL_SERV, PRODUCT_FOOD_SHOP_SERV])) {
            if ($serv_type == PRODUCT_TRAVEL_OIL_SERV) {
                $type = CouponBrand::TYPE_TRAVEL;
            } else{
                $type = CouponBrand::TYPE_UNION_FOOD;
            }
            $params['type'] = $type;
        }
        $data = CouponBrand::saveBrand($params,"edit_brand");
        return $data;
   }

    //修改品牌状态
    public function setStatus(Request $request){
        $params = $request->only(['id', 'status']);
        $data = CouponBrand::saveBrand($params,"set_status");
        return $data;
    }

    //返回添加关联品牌时下拉选项
    public function brandSelect()
    {
        return (new CouponBrand())->brandSelect();
    }

    /**
     * 新增充值类型
     * @method post
     * @params array $params
     * returns
     */
    public function add(Request $request, BrandValidate $validate)
    {
        $params = $request->only(['name','image_url','status','sort', 'discount_logo']);
        $result = $validate->scene('add_brand')->check($params);
        if (!$result) {
            return fail($validate->getError());
        }
        //判断品牌类型是否存在或者是否被删除
        $brandModel = new CouponBrand();
        $info = $brandModel->where('name', trim($params['name']))->where('status',1)->find();
        if ($info) {
            return fail('新增失败，品牌已存在');
        }
        $params['icon'] = $params['image_url'];
        $data = CouponBrand::create($params);
        return success('新增成功', ['id' => $data->id]);
    }

    /**
     * 权益图标管理
     * @method get
     * @return \think\response\Json
     */
    public function brandIconList(Request $request){
        $result = json_decode(($this->getCateAndSubBrand())->getContent(),true);
        $brand_ids = array();
        foreach ($result['data'] as $cateBrand) {
            foreach ($cateBrand['couponBrand'] as $couponBrand) {
               array_push($brand_ids,$couponBrand['id']);
            }
        }
        $brand_ids = array_values(array_unique($brand_ids));
        $params = $request->param();
        $params['status'] = CouponBrand::STATUS_ACTIVE;
        $params['belong'] = 'all';
        $params['serv_type'] = 'yes';
        $params['brand_ids'] = $brand_ids;
        $data = CouponBrand::brandList($params);
        return success('查询成功', $data);
    }

    /**
     * 权益图标管理编辑保存
     * @method post
     * @return \think\response\Json
     */
    public function iconSave(Request $request)
    {
        $params = $request->param();
        /*if (($params['subtitle_type'] == CouponBrand::SUBTITLE_TYPE_ZDY) && (!isset($params['subtitle']) || empty($params['subtitle']))) {
            return fail('副标题类型为自定义，副标题内容不能为空');
        }*/
        $info = CouponBrand::query()->where('id', $params['id'])->find();
        if (!$info) {
            return fail('品牌不存在');
        }
        //日常出行/餐饮美食副标题只支持自定义内容
        if (in_array($info->type, [CouponBrand::TYPE_UNION_FOOD, CouponBrand::TYPE_TRAVEL]) && $params['subtitle_type'] == CouponBrand::SUBTITLE_TYPE_ZK) {
            return fail('日常出行/餐饮美食副标题只支持自定义内容');
        }
        $info = CouponBrand::update($params);
        return success('保存成功', ['id' => $info->id]);
    }


    public function getServType()
    {
        $servTypeArr = PRODUCT_SERV_TYPE;
        $temp = array();
        foreach ($servTypeArr as $k => $v) {
            $temp[] = [
                'serv_type' => $k,
                'name' => $v['name'],
            ];
        }
        return success('查询成功', $temp);
    }
}
